package dy.com.tcp结合udp.Server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    private ClientListener clientListener = null;
    int listenPort;

    public TCPServer(int listenPort) {
        this.listenPort = listenPort;
    }

    void start() throws IOException {
        //先停止监听
        stop();
        //新建监听线程
        System.out.println("启动监听线程");
        clientListener = new ClientListener(listenPort);
        clientListener.start();

    }

    //停止监听线程
    void stop() {
        if (clientListener != null) {
            clientListener.close();
            clientListener = null;
        }
    }

    //监听线程
    private static class ClientListener extends Thread {
        int listenPort;
        private ServerSocket serverSocket;
        boolean finish;

        ClientListener(int listenPort) throws IOException {
            this.listenPort = listenPort;
            finish = false;

            serverSocket = new ServerSocket(listenPort);
        }

        @Override
        public void run() {
            //新建serversocket
            System.out.println("tcp服务器监听线程正在run");


            System.out.println("tcp服务器信息:ip:" + serverSocket.getInetAddress() +
                    ",port:" + serverSocket.getLocalPort() + "，监听的端口是:" + listenPort);

            while (!finish) {
                Socket clientSocket = null;
                try {
                    clientSocket = serverSocket.accept();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ClientHandler clientHandler = new ClientHandler(clientSocket);
                clientHandler.start();
            }

            System.out.println("监听服务已经关闭");
        }

        private void close() {
            finish = true;
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //客户端socket处理线程
    private static class ClientHandler extends Thread {
        Socket clientSocket = null;
        boolean stop = false;

        ClientHandler(Socket clientSocket) {
            this.clientSocket = clientSocket;
        }

        @Override
        public void run() {
            System.out.println("开始处理新的客户端连接:" + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());

            try {
                //得到缓存reader
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

                //得到打印流
                PrintStream printStream = new PrintStream(clientSocket.getOutputStream());

                while (!stop) {
                    String s = bufferedReader.readLine();
                    if (s.equals("bye")) {
                        stop = true;
                        printStream.println("bye");
                    } else {
                        System.out.println("收到数据为:" + s);
                        printStream.println(s.length());
                    }
                }
                bufferedReader.close();
                printStream.close();


            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("客户端已退出");
        }
    }
}
